package top.jamsee.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import top.jamsee.dao.BuyDao;
import top.jamsee.dao.LentDao;
import top.jamsee.entity.Buy_data;
import top.jamsee.entity.FileInfo;
import top.jamsee.entity.Lent_data;
import top.jamsee.entity.ResultInfo;
import top.jamsee.service.BuyService;
import top.jamsee.service.LentService;
import top.jamsee.util.fileUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class LentServiceImpl implements LentService {
    @Resource
    private LentDao dao;

    @Override
    //查询租房表的数据
    public List<List> SelectLentData(int pageNum) {


        //当前页数
//        System.out.println(p.getPageNum());
        //获取全部页数
        //每页的大小
//        System.out.println(p.getPageSize());
        //总页数
        Page p= PageHelper.startPage(pageNum,20);
        List<Lent_data> data=dao.selectLentData();
        //算出多少页=全部多少行,每页多少条
        int colunms=dao.selectLentNum();
        int pages=colunms/20;

        List<List> ls=new ArrayList<List>();
        List<Integer> pageList=new ArrayList<Integer>();
        pageList.add(pages);
        ls.add(data);
        ls.add(pageList);




        //返回

        return ls;
    }

    //删除租房表的数据
    @Override
    public int deleteLentData(int id){
        int res=dao.deleteLentDataById(id);//
        System.out.println("结果是"+res);
        return res;
    }

    @Override
    public int selectLentNum() {
        return dao.selectLentNum();
    }

    //添加一条记录(需要操作两个表 需要事务)
    @Override
    public ResultInfo addLentData(Lent_data lent_data, HttpServletRequest request){
        String jsonFileAddress="";
        //there is fileupload
        String fileName= null;
        String jsonFileMessage="";
        try {
            fileName = fileUpload.uploadFile(request);
        } catch (Exception e) {
            e.printStackTrace();
            //插入失败
            return new ResultInfo(false,"文件上传失败");

        }
        System.out.println("文件上传成功");
        System.out.println(fileName);
        //文件上传发生异常时
        if ("none".equals(fileName)){
            return new ResultInfo(false,"文件上传失败");
        }
        //没有上传文件时
        if ("".equals(fileName)){
            return new ResultInfo(false,"请添加文件进行上传");
        }



        ObjectMapper om=new ObjectMapper();
        //文件的json信息,考虑到后面要上传多个文件
        System.out.println("file fail");
        FileInfo fl=new FileInfo();
        System.out.println("file fail1");
        ArrayList sl=new ArrayList();
        System.out.println("file fail2");
        sl.add(0,fileName);
        System.out.println("file fail3");
        fl.setFileName(sl);
        System.out.println("file fail4");
        try {
            System.out.println("json fail");
            jsonFileMessage=om.writeValueAsString(fl);

        } catch (JsonProcessingException e) {
            System.out.println("json fail");
            e.printStackTrace();
        }
        //get filename
        lent_data.setImgAddress(jsonFileMessage);

        int addRes=dao.addLentData(lent_data);
        //there


        return new  ResultInfo(true,"成功插入数据");

    }

    @Override
    //模糊查询buy表
    public List selectLentByData(int pageNum,String data) {


        //当前页数
//        System.out.println(p.getPageNum());
        //获取全部页数
        //每页的大小
//        System.out.println(p.getPageSize());
        //总页数
        Page p=PageHelper.startPage(pageNum,20);
        List<Lent_data> lent_data=dao.selectLentByData(data);
        //算出多少页=全部多少行,每页多少条
        int colunms=dao.selectLentByDataNum(data);
        int pages=colunms;

        List ls=new ArrayList();
        ls.add(lent_data);
        ls.add(pages);



        //返回
        return ls;


    }
    //模糊查询lent表的所有数量方便后续分页
    @Override
    public int selectLentByDataNum(String data ){

        return dao.selectLentByDataNum(data);
    }
    //更新lent表根据id
    @Override
    public ResultInfo upDateLentById(Lent_data lent_data, HttpServletRequest request){
        String jsonFileAddress="";
        //there is fileupload
        String fileName= null;
        String jsonFileMessage="";
        try {
            fileName = fileUpload.uploadFile(request);
        } catch (Exception e) {
            e.printStackTrace();
            //插入失败
            return new ResultInfo(false,"文件更新失败");

        }
        System.out.println("文件上传成功");
        System.out.println(fileName);
        //文件上传发生异常时
        if ("none".equals(fileName)){
            return new ResultInfo(false,"文件更新失败");
        }
        //没有上传文件时
        if ("".equals(fileName)){
            return new ResultInfo(false,"请添加文件进行更新");
        }



        ObjectMapper om=new ObjectMapper();
        //文件的json信息,考虑到后面要上传多个文件
        FileInfo fl=new FileInfo();
        ArrayList sl=new ArrayList();
        sl.add(0,fileName);
        fl.setFileName(sl);

        try {
            jsonFileMessage=om.writeValueAsString(fl);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //get filename
        lent_data.setImgAddress(jsonFileMessage);

        int addRes=dao.upDateLentById(lent_data);
        //there


        return new  ResultInfo(true,"成功更新数据");
    }
    //根据id查询是否有此条记录,更新表时使用
    @Override
    public int selectLentById(int id){ return dao.selectLentById(id); }
}


